/* rcoqtya.p - DETERMINE OPEN QUANTITY FOR A SCHEDULED ORDER LINE             */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.6.2.2 $                                                         */
/*V8:ConvertMode=NoConvert                                                    */
/* REVISION: 8.5      LAST MODIFIED: 12/13/95         BY: GWM *J049*          */
/* REVISION: 8.5      LAST MODIFIED: 05/29/96         BY: AME *J0NP*          */
/* REVISION: 8.6      LAST MODIFIED: 09/20/96         BY: TSI *K005*          */
/* REVISION: 8.6      LAST MODIFIED: 05/12/97  BY: *K0D5* Vinay Nayak-Sujir   */
/* REVISION: 8.6      LAST MODIFIED: 05/20/98  BY: *K1Q4* Alfred Tan          */
/* REVISION: 8.6      LAST MODIFIED: 09/14/98  BY: *J2ZS* Jean Miller         */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00  BY: *N0KP* Mark Brown          */
/* $Revision: 1.6.2.2 $    BY: Jean Miller         DATE: 03/22/01 ECO: *P008*     */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/*INPUT PARAMETERS*/
define input parameter sod_recid as recid.
define input parameter first_date as date.
define input parameter last_date as date.
define input parameter first_time as character no-undo.
define input parameter last_time as character no-undo.
define input parameter first_ref like schd_reference no-undo.
define input parameter last_ref like schd_reference no-undo.

/*OUTPUT PARAMETERS*/
define output parameter open_qty like sod_qty_ord.

/*LOCAL VARIABLES*/
define variable cum_net_req as decimal no-undo.
define variable net_req as decimal no-undo.
define variable prior_cum_net_req as decimal no-undo.
define variable ship_cmplt as decimal no-undo.
define variable cum_all_qty as decimal no-undo.

find sod_det where recid(sod_det) = sod_recid no-lock.

find so_mstr where so_nbr = sod_nbr no-lock.

assign
   ship_cmplt = if so_ship_cmplt = 0 then
                   100
                else
                   so_ship_cmplt
   open_qty   = 0.

find sch_mstr where
     sch_type = 3
 and sch_nbr = sod_nbr
 and sch_line = sod_line
 and sch_rlse_id = sod_curr_rlse_id[3]
no-lock no-error.

for each schd_det
   where schd_type = sch_type
     and schd_nbr = sch_nbr
     and schd_line = sch_line
     and schd_rlse_id = sch_rlse_id
     and schd_date <= last_date
no-lock:

   {rcoqty.i}

   /* Logic: Consider first_time only for schedules on first_date and
    * last_time only for schedules on the last_date.
    * If the schedule is between the first_date and last_date,
    * then ignore the times.  If the first_date and last_date coincide, then
    * consider both times to select schedules. Some apparently redundant
    * logic like schd_date = first_date and schd_date < last_date
    * is required to catch user entering first_date which is
    * greater than the last_date */
   if net_req <> 0
      and ((schd_date = first_date and schd_date < last_date
      and schd_time >= first_time) or
      (schd_date > first_date and schd_date < last_date) or
      (schd_date > first_date and schd_date = last_date
      and schd_time <= last_time) or
      (schd_date = first_date and schd_date = last_date and
      schd_time >= first_time and schd_time <= last_time))
      and schd_reference >= first_ref and schd_reference <= last_ref
   then
      open_qty = open_qty + net_req.

end.

/* Check if no open quantity and Cum Qty Acct */
if open_qty = 0
   and so_cum_acct
   and sch_pcs_date >= first_date
   and sch_pcs_date <= last_date
then
   open_qty = max(sch_pcr_qty - sod_cum_qty[1],0).
